package dataPrp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Seperator {
	
	public static String movieFile = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/movie_titles.txt";
	public static String trainingSetPath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/training_set/";
	public static String testFile = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/probe.txt";
	public static String outputPath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/training_set_new/";
	public static String testScoreFile = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/probe_score.txt";
	public static String newProbePath = "/Users/macpro724/Desktop/ml_prj/data/netflix/download/probe_combine.txt";
	
	public Map<Integer, List<Integer>> testSet;
	public int rating_count = 0;
	public void importTest(){
		testSet = new HashMap<Integer, List<Integer>>();
		try{
			BufferedReader br = new BufferedReader(new FileReader(Seperator.testFile));
			List<Integer> tempList = null;
			String line;
			
			int movieId = 0;
			int userId = 0;
			
			while ((line = br.readLine()) != null){
				if (line.endsWith(":")){
					movieId = Integer.parseInt(line.substring(0, line.length()-1));
					if (!testSet.containsKey(movieId)){
						tempList = new ArrayList<Integer>();
						testSet.put(movieId, tempList);
					}
				}else{
					tempList = testSet.get(movieId);
					userId = Integer.parseInt(line);
					tempList.add(userId);
					testSet.put(movieId, tempList);
				}
			}
			
			br.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void seperate(){
		File directory = new File(Seperator.trainingSetPath);
		File[] f = directory.listFiles();
		

		
		for (File fs : f){
			try{
				System.out.println(fs.getName());
				BufferedReader br = new BufferedReader(new FileReader(fs));
				List<Integer> tempList = null;
				String line;
				
				int movieId = 0;
				int userId = 0;
				
				line = br.readLine();
				if (line.endsWith(":")){
					movieId = Integer.parseInt(line.substring(0, line.length()-1));
					if (testSet.containsKey(movieId)){
						tempList = testSet.get(movieId);
						BufferedWriter bw = new BufferedWriter(new FileWriter(outputPath + "movie_" + movieId + ".txt"));
						bw.write(line + "\n");
						while ((line = br.readLine()) != null){
							userId = Integer.parseInt(line.substring(0, line.indexOf(",")));
							if (!tempList.contains(userId)){
								rating_count++;
								if (rating_count % 10000 == 0) System.out.println(rating_count);
								bw.write(line + "\n");
							}
						}
						bw.close();
					}else{
						BufferedWriter bw = new BufferedWriter(new FileWriter(outputPath + "movie_" + movieId + ".txt"));
						bw.write(line + "\n");
						while ((line = br.readLine()) != null){
							rating_count++;
							if (rating_count % 10000 == 0) System.out.println(rating_count);
							bw.write(line + "\n");
						}
						bw.close();
					}
				}
				

				
				br.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public void addScore(){
		try{
			BufferedReader br = new BufferedReader(new FileReader(Seperator.testFile));
			BufferedWriter bw = new BufferedWriter(new FileWriter(Seperator.testScoreFile));
			String line;
			
			int movieId = 0;
			int userId = 0;
		
			line = br.readLine();
			while (line != null){
				if (line.endsWith(":")){
					movieId = Integer.parseInt(line.substring(0, line.length()-1));
					bw.write(line + "\n");
					line = br.readLine();
				}else{
					String tempString = String.valueOf(movieId);
					int length = tempString.length();
					for (int i = 0; i < 7-length; i++){
						tempString = "0" + tempString;
					}
					
//					System.out.println(movieId + "\t" +tempString);
					
					Map<Integer, Double> result = getScoreMap(tempString);
					
					while ((line != null) && !line.endsWith(":")){
						
						userId = Integer.parseInt(line);
						
						bw.write(String.valueOf(result.get(userId)) + "\n");
						
						line = br.readLine();
					}
					
					
				}
				
			}
			bw.flush();
			bw.close();
			br.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public Map<Integer, Double> getScoreMap(String path){
		Map<Integer, Double> result = new HashMap<Integer, Double>();
		try{
			BufferedReader bb = new BufferedReader(new FileReader(Seperator.trainingSetPath + "mv_" + path + ".txt"));
			
			
			
			String line = bb.readLine();
			
			while ((line = bb.readLine()) != null){
				String[] s = line.split(",");
				result.put(Integer.parseInt(s[0]), Double.parseDouble(s[1]));
			}
			
			bb.close();
		}catch (Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	public void freshProbe(){
		
		try{
			
			BufferedWriter bw = new BufferedWriter(new FileWriter(Seperator.newProbePath));
			BufferedReader br1 = new BufferedReader(new FileReader(Seperator.testFile));
			BufferedReader br2 = new BufferedReader(new FileReader(Seperator.testScoreFile));
			
			String line1 = br1.readLine();
			String line2 = br2.readLine();
			
			while (line1 != null && line2 != null){
				if (line1.endsWith(":")) bw.write(line1 + "\n");
				else bw.write(line1 + "," + line2 + "\n");
				line1 = br1.readLine();
				line2 = br2.readLine();
			}
			
			br1.close();
			br2.close();
			bw.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args){
		Seperator sp = new Seperator();
//		sp.importTest();
//		sp.seperate();
//		sp.addScore();
		sp.freshProbe();
//		System.out.println(sp.rating_count);
	}
	
}
